<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/*
 * 单图片上传并生成一张缩略图
 */
function uploadOne($controller='',$thumbDir='',$filename='pic',$thumbWidth=120,$thumbHeight=80){
    //传入图片上传表单name值
    $file = request()->file($filename);
    //移动到指定路径
    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $controller);
    //如果移动失败,输出报错
    if(!$info){
        $this->error($file->getError());
    }
    //获取上传后图片的全部路径
    $pic = 'uploads'. DS .$controller. DS .$info->getSavename();
    //指定缩略图保存目录
    $thumb_pic = 'uploads'. DS .$controller. DS .$thumbDir. DS .$info->getFilename();
    if(!is_dir('uploads'. DS .$controller. DS .$thumbDir) ){
        mkdir('uploads'. DS .$controller. DS .$thumbDir,0777,true);
    }
    //引入图片处理类
    $image = \think\Image::open($pic);
    //指定尺寸处理图片,并保存到指定目录
    $image->thumb($thumbWidth,$thumbHeight,\think\Image::THUMB_SOUTHEAST)->save($thumb_pic);
    //返回原始图片路径与缩略图路径
    $fileInfo['pic'] = $pic;
    $fileInfo['thumb_pic'] = $thumb_pic;
    return $fileInfo;
}



//阿里短信，mobile为手机号码，code为自定义随机数
function sendMsg($mobile,$code){

    //或者引入命名空间use alisms\SendSms;  然后直接实例化$sms = new SendSms();
    $sms = new \alisms\SendSms();
    $sms->accessKeyId = 'LTAIAJkaWtPLXZfS';
    $sms->accessKeySecret = 'wiGeLuj5hlHXNenweTnPrmuecRJPsW';
    $sms->signName = '浪博电子';
    $sms->templateCode = 'SMS_109385462';
    
    $templateParam = array("code"=>$code);
    $m = $sms->send($mobile,$templateParam);
    return $m;
}
function ajaxMsg($status,$msg)
{
    $data['status'] = $status;
    $data['msg'] = $msg;
    return json($data);
}

function download($file_url,$new_name=''){
    if(!isset($file_url)||trim($file_url)==''){
        return false;
    }
    if(!file_exists($file_url)){ //检查文件是否存在
        return false;
    }
    $file_name=basename($file_url);
    $file_type=explode('.',$file_url);
    $file_type=$file_type[count($file_type)-1];
    $file_name=trim($new_name=='')?$file_name:urlencode($new_name);
    $file_type=fopen($file_url,'r'); //打开文件
    //输入文件标签
    header("Content-type: application/octet-stream");
    header("Accept-Ranges: bytes");
    header("Accept-Length: ".filesize($file_url));
    header("Content-Disposition: attachment; filename=".$file_name);
    //输出文件内容
    echo fread($file_type,filesize($file_url));
    fclose($file_type);


}